package com.cunjin.admin.comtroller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cunjin.admin.entity.AdminEntity;
import com.cunjin.admin.service.AdminService;
import com.cunjin.base.controller.BaseController;

@Controller
@RequestMapping("/admin")
public class AdminController extends BaseController<AdminEntity> {
	
	@Autowired
	private AdminService service;
	
	//http://localhost:8090/cunjin-dorm/admin/myqueryAll
	@RequestMapping("/myqueryAll")
	@ResponseBody
	public Map<String, Object> myqueryAll(AdminEntity entity) {
		
		Map<String, Object> all = service.queryAll();
		return all;
	}
		
	//http://localhost:8090/cunjin-dorm/admin/mylogin
	@RequestMapping("/mylogin")
	@ResponseBody
	public Map<String, Object>  mylogin(AdminEntity entity,HttpServletRequest req) {
		
		Map<String, Object>  myinsert = service.login(entity,req);
		return myinsert;
	}
	
	@RequestMapping("/reg")
	@ResponseBody
	public Map<String, Object>  regUser(AdminEntity entity) {
		
		Map<String, Object>  map = service.regUser(entity);
		return map;
	}
		
		
	/**
	 * 分页
	 * @param page
	 * @param rows
	 * @param searchName
	 * @return
	 */
	@RequestMapping("/findWithPage")
	@ResponseBody
	public Map<String, Object>  findWithPage(int page,int rows,String searchName) {
	
		Map<String, Object>  fy = service.queryByPage(page,rows,searchName);
		return fy;
	}
	
	/**
	 * 添加
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping("/addAdmin")
	@ResponseBody
	public Map<String, Object> addAdmin(AdminEntity entity) {
		
		Map<String, Object>  map = service.insert(entity);
				return map;
	}
	
	
	/**
	 * 
	 * 删除
	 * @param dles
	 * @return
	 */
	@RequestMapping("/delAdmin")
	@ResponseBody
	public Map<String, Object>  delAdmin(String dles) {
		
		String[] split = dles.split(",");
		Map<String, Object>  mydelete = service.deleteByids(split);
		return mydelete;
	}

	/**
	 * 更新
	 * @param entity
	 * @return
	 */
	@RequestMapping("/updateAdmin")
	@ResponseBody
	public Map<String, Object>  updateAdmin(AdminEntity entity) {
		
		Map<String, Object>  myupdate = service.update(entity);
		return myupdate;
	}
	
	/**
	 * 退出
	 * @param entity
	 * @return
	 */
	@RequestMapping("/quitlogin")
	@ResponseBody
	public Map<String, Object>  quitlogin(HttpServletRequest req) {
		Map<String, Object> map=new HashMap<String, Object>(); 
		HttpSession session = req.getSession();
		session.removeAttribute("username");
		session.removeAttribute("loginNo");
		map.put("code", 200);
		return map;
	}

}
